﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Company.Web.Commen;
using Code.Commen;
using Code.BLL;
using Code.Models;
using System.Web.Security;

namespace Company.Web.Areas.Manage.Controllers
{
    public class ControllerBase:Controller
    {
        private LoginAdmin _admin;
        protected LoginAdmin Admin
        {
            get
            {
                if (_admin == null)
                    _admin = InlineAdmin[AdminID];
                return _admin;
            }
        }
        private Guid AdminID { get; set; }

        protected HashTable<Guid, LoginAdmin> InlineAdmin { get { return Company.Web.Global.InlineAdmin; } }

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (User == null || !User.Identity.IsAuthenticated || !User.IsInRole("Manage"))
            {
                filterContext.Result = RedirectToAction("index", "login", new { @return = Request.RawUrl });
                return;
            }
            
            AdminID = new Guid(User.Identity.Name);
            Session["AdminID"] = AdminID;

            if (Admin == null || Admin.Reload || Admin.LoadTime.AddSeconds(5) < DateTime.Now)
            {
                var MM = new ManagerManage();
                var a = MM.GetTable<Admin>().FirstOrDefault(p => p.ID == AdminID);
                if (a == null)
                {
                    filterContext.Result = RedirectToAction("index", "login", new { @return = Request.RawUrl });
                    return;
                }
                var admin = new LoginAdmin
                {
                    ID = AdminID,
                    LoginName=a.LoginName,
                    State=(AdminState)a.State,
                    LoadTime=DateTime.Now,
                    SessionID=Session.SessionID,
                    LoginIP=Request.UserHostAddress,
                };
                InlineAdmin[AdminID] = admin;
            }
            if (Admin.State == AdminState.Disabled)
            {
                string str = string.Format("<script>alert('该的账号已被锁定\\r\\n无法使用！');location.href='{0}';</script>", Url.Action("index", "login", new { @return = Request.RawUrl }));
                filterContext.Result = Content(str);
                FormsAuthentication.SignOut();
                return;
            }
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            ViewData["Route_ID"] = RouteData.Values["id"];
            ViewData["Route_A"] = RouteData.Values["a"];
            ViewData["Route_B"] = RouteData.Values["b"];
            RouteData.Values.Remove("id");
            RouteData.Values.Remove("a");
            RouteData.Values.Remove("b");
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            var log = Log.GetLog<ControllerBase>();

            log.Error(filterContext.Exception);

            filterContext.Result = View("Error", filterContext.Exception);
            filterContext.ExceptionHandled = true;
        }
    }
}